
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import cn.hutool.core.date.DatePattern;
import com.hlkj.pay.app.common.IDownloadExcelService;
import com.hlkj.pay.infrastructure.model.order.OrderTransferDO;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.vo.order.resp.PageOrderAndStatisticsResp;
import com.hlkj.pay.vo.order.resp.PageWithdrawAndStatisticsResp;
import com.hlkj.pay.vo.order.resp.export.PayOrderFileExport;
import com.hlkj.pay.vo.order.resp.export.WithdrawOrderFileExport;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.order.IWithdrawOrderAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.order.OrderWithdrawDto;
import com.hlkj.pay.dto.order.WithdrawOrderQueryDto;
import com.hlkj.pay.infrastructure.model.merchant.MerchantReportInfoDO;
import com.hlkj.pay.infrastructure.model.order.OrderWithdrawDO;
import com.hlkj.pay.service.merchant.IMerchantReportService;
import com.hlkj.pay.service.order.IWithdrawOrderService;
import com.hlkj.pay.vo.order.req.OrderWithdrawReq;
import com.hlkj.pay.vo.order.req.WithdrawOrderPageQueryReq;
import com.hlkj.pay.vo.order.resp.OrderWithdrawResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "汇电通提现订单管理")
@RestController
@RequestMapping("/v1/order/withdraw")
@RequiredArgsConstructor
@Slf4j
public class WithdrawOrderController extends BaseController {

    private final IWithdrawOrderService withdrawOrderService;

    private final IWithdrawOrderAppService withdrawOrderAppService;

    private final IMerchantReportService merchantReportService;

    private final IDownloadExcelService downloadExcelService;

    @Operation(summary = "订单查询")
    @GetMapping("/page")
    CommonResult<PageWithdrawAndStatisticsResp> queryPayOrderPage(WithdrawOrderPageQueryReq withdrawOrderPageQueryReq) {
        PageWithdrawAndStatisticsResp withdrawAndStatisticsResp = null;
        WithdrawOrderQueryDto commonOrderQueryDto = BeanUtil.copy(withdrawOrderPageQueryReq, WithdrawOrderQueryDto.class);
        processQueryDate(withdrawOrderPageQueryReq, commonOrderQueryDto);
        PageResult<OrderWithdrawDto> page = withdrawOrderService.queryPage(commonOrderQueryDto);
        List<OrderWithdrawDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            CommonResult<OrderWithdrawResp> result = buildPageResult(page, Collections.emptyList());
            withdrawAndStatisticsResp = BeanUtil.copyProperties(result.getData(), PageWithdrawAndStatisticsResp.class);
            return CommonResult.success(withdrawAndStatisticsResp);
        }
        if (withdrawOrderPageQueryReq.getExport() == 1) {
            String fileName = withdrawOrderPageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "提现导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, commonOrderQueryDto, WithdrawOrderFileExport.class);
            return CommonResult.success();
        } else {
            List<OrderWithdrawResp> payOrderRespList = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderWithdrawResp.class);
            processMerchantList(payOrderRespList);

            CommonResult<OrderWithdrawResp> result = buildPageResult(page, payOrderRespList);
            withdrawAndStatisticsResp = BeanUtil.copyProperties(result.getData(), PageWithdrawAndStatisticsResp.class);
            withdrawAndStatisticsResp.setOrderStatisticsDO(withdrawOrderService.withdrawStaticOrdersPage(commonOrderQueryDto));
            return CommonResult.success(withdrawAndStatisticsResp);
        }
    }

    @Operation(summary = "订单详情")
    @GetMapping("/detail")
    CommonResult<OrderWithdrawResp> queryPayOrderDetail(@RequestParam("payOrderNo") String payOrderNo) {
        OrderWithdrawDO orderWithdrawDO = withdrawOrderService.queryPayOrder(payOrderNo);
        if (orderWithdrawDO == null) {
            return CommonResult.success(null);
        }
        withdrawOrderAppService.queryPayStatus(orderWithdrawDO);
        OrderWithdrawResp payOrderDetailResp = BeanUtil.copyProperties(orderWithdrawDO, OrderWithdrawResp.class);
        MerchantReportInfoDO inMerchantReportInfoDO = merchantReportService.detailByUserId(orderWithdrawDO.getUserId());
        if (inMerchantReportInfoDO != null) {
            payOrderDetailResp.setUserName(inMerchantReportInfoDO.getName());
            payOrderDetailResp.setVirtualAccountNo(inMerchantReportInfoDO.getVirtualAccountNo());
            payOrderDetailResp.setBankName(inMerchantReportInfoDO.getBankName());
        }
        return CommonResult.success(payOrderDetailResp);
    }

    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    CommonResult<OrderWithdrawResp> submitOrder(@RequestBody @Validated OrderWithdrawReq orderWithdrawReq) {
        return withdrawOrderAppService.submitOrder(orderWithdrawReq);
    }

    @Operation(summary = "状态同步")
    @GetMapping("/status")
    CommonResult<Void> status(@RequestParam("payOrderNo") String payOrderNo) {
        OrderWithdrawDO orderWithdrawDO = withdrawOrderService.queryPayOrder(payOrderNo);
        if (orderWithdrawDO == null) {
            return CommonResult.success(null);
        }
        withdrawOrderAppService.queryPayStatus(orderWithdrawDO);
        return CommonResult.success();
    }
}
